<?php 
include("QRcode.php");
//获取项目根目录
define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/");
echo _set_color_qrcode('123456789', '#ff0000','#00FA9A');
/**
 * @author fld 2021-02-08
 * @desc 生成二维码
 * @param $qrcode_content   二维码内容
 * @param string $back_color    二维码背景颜色
 * @param string $line_color    二维码线条颜色
 * @return string
 */
function _set_color_qrcode($qrcode_content, $back_color="#ffffff", $line_color = "#000000"){
	$object = new QRcode();
	$level = 3;
	$size = 10;
	$errorCorrectionLevel =intval($level) ;//容错级别
	$matrixPointSize = intval($size);//生成图片大小
	//获取图片保存地址
	$image_dir = BASE_PATH.'image/'.date('Ymd').'/';
	if(!is_dir($image_dir)){
		mkdir(iconv("UTF-8", "GBK", $image_dir),0777,true);
	}   
	//图片名称
	$file_name = md5('qrcode_'.$qrcode_content.time()).'.png';
	$back_array = hex2rgb($back_color);
	$line_array = hex2rgb($line_color);
	//二维码生成
	$object->pngcolor($qrcode_content, $image_dir.$file_name, $errorCorrectionLevel, $matrixPointSize, 2 , false, $back_array, $line_array);
	
	return $image_dir.$file_name;
}

/**
 * 十六进制转RGB
 * @param string $color 16进制颜色值
 * @return array
 */
function hex2rgb($color) {
	$hexColor = str_replace('#', '', $color);
	$lens = strlen($hexColor);
	if ($lens != 3 && $lens != 6) {
		return false;
	}
	$newcolor = '';
	if ($lens == 3) {
		for ($i = 0; $i < $lens; $i++) {
			$newcolor .= $hexColor[$i] . $hexColor[$i];
		}
	} else {
		$newcolor = $hexColor;
	}
	$hex = str_split($newcolor, 2);
	$rgb = [];
	foreach ($hex as $key => $vls) {
		$rgb[] = hexdec($vls);
	}
	return $rgb;
}
?>